package com.tensquare.spit.service;

import com.tensquare.spit.dao.SpitDao;
import com.tensquare.spit.pojo.Spit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.parsing.Problem;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import util.IdWorker;

import java.util.*;
import java.util.function.Function;

@Service
public class SpitService {
    @Autowired
    private SpitDao spitDao;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private MongoTemplate mongoTemplate;

    public List<Spit> findAll() {
       return spitDao.findAll();
    }

    public Spit findById(String spitId) {
        return spitDao.findById(spitId).get();
    }

    public void save(Spit spit) {
        spit.set_id(idWorker.nextId()+"");
        spit.setPublishtime(new Date());//发布日期
        spit.setVisits(0);//浏览量
        spit.setShare(0);//分享数
        spit.setThumbup(0);//点赞数
        spit.setComment(0);//回复数
        spit.setState("1");//状态

        if(spit.getParentid()!=null && !"".equals(spit.getParentid())){
            //给父节点吐槽的回复数加一
            Query query = new Query();
            query.addCriteria(Criteria.where("_id").is(spit.getParentid()));
            Update update = new Update();
            update.inc("comment", 1);
            mongoTemplate.updateFirst(query, update, "spit");
        }
        spitDao.save(spit);
    }

    public void update(Spit spit) {
        spitDao.save(spit);
    }
    public void deleteById(String id){
        spitDao.deleteById(id);
    }
    public Page<Spit> pageQuery(String parentid, int page, int size){
        Pageable pageable = PageRequest.of(page-1, size);
        return spitDao.findByParentid(parentid, pageable);
    }

    public void addthumbup(String id) {
        Query query=new Query();
        query.addCriteria(Criteria.where("_id").is(id));
        Update update=new Update();
        update.inc("thumbup",1);
        mongoTemplate.updateFirst(query,update,"spit");
    }

    public  Page<Spit> findByPage(Map<String, Object> map, Integer page, Integer size) {

        Query query=new Query();
        Set<String> set = map.keySet();
        for (String s : set) {
            query.addCriteria(Criteria.where(s).is(map.get(s)));
        }
        Pageable pageable= PageRequest.of(page-1,size);
        query.with(pageable);
        List<Spit> spit = mongoTemplate.find(query, Spit.class, "spit");
        Page page1=new PageImpl(spit);
        return page1;


    }

    public List<Spit> findBySearch(Map<String, Object> map) {
        Query query=new Query();
        Set<String> set = map.keySet();
        for (String s : set) {
            query.addCriteria(Criteria.where(s).is(map.get(s)));
        }
        return mongoTemplate.find(query,Spit.class,"spit");
    }
}
